package com.lb.jwitter.action;


import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport; 
 import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocBatBuocService;
import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyet;
import com.lb.jwitter.service.TblQLDTTXAMonHocTienQuyetService;
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;


import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;


import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHoc;


 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienPhieuDangKyMonHocEditFormAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private int id; 
    public void setId(int id) { this.id = id; }

    public int getId() { return id; }
    
    private int hocVienId; 
    public void setHocVienId(int hocVienId) { this.hocVienId = hocVienId; }

    public int getHocVienId() { return hocVienId; }
    
    private int hocKyId; 
    public void setHocKyId(int hocKyId) { this.hocKyId = hocKyId; }

    public int getHocKyId() { return hocKyId; }

    private TblQLDTTXAHocVienPhieuDangKyMonHoc tblQLDTTXAHocVienPhieuDangKyMonHoc; 
    public TblQLDTTXAHocVienPhieuDangKyMonHoc getTblQLDTTXAHocVienPhieuDangKyMonHoc() { 
        return tblQLDTTXAHocVienPhieuDangKyMonHoc; 
    } 
    private List<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs; 
    public List<TblQLDTTXAHocVienPhieuDangKyMonHoc>  getTblQLDTTXAHocVienPhieuDangKyMonHocs() {
         return tblQLDTTXAHocVienPhieuDangKyMonHocs; 
      } 

    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService; 


    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private List<TblQLDTTXAHocVienDangKyMonHoc> tblQLDTTXAHocVienDangKyMonHocs; 
    public List<TblQLDTTXAHocVienDangKyMonHoc>  getTblQLDTTXAHocVienDangKyMonHocs(){ 
              return tblQLDTTXAHocVienDangKyMonHocs; 
     } 
    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocThieuMonTienQuyets; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocThieuMonTienQuyets(){ 
              return tblQLDTTXAMonHocThieuMonTienQuyets; 
     }
    
    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocThieuMonHocTruocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocThieuMonHocTruocs(){ 
              return tblQLDTTXAMonHocThieuMonHocTruocs; 
    }
    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService;
    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService; 
    
    private TblQLDTTXAMonHocBatBuocService tblQLDTTXAMonHocBatBuocService;
    private TblQLDTTXAMonHocTienQuyetService tblQLDTTXAMonHocTienQuyetService;
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;

    private List<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public List<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "sua")){ 
        return "noprivilege"; 
    }        
    tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 

        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 

        if (id != 0){
        	tblQLDTTXAHocVienPhieuDangKyMonHoc = tblQLDTTXAHocVienPhieuDangKyMonHocService.gettblQLDTTXAHocVienPhieuDangKyMonHoc(new Long(id));
        } else {
        	List<ConditionReport> cons = new ArrayList<ConditionReport>();
        	if (hocVienId != 0){
 			   ConditionReport con = new ConditionReport(); 
 		       con.setCol("hocVienId.id"); 
 		       con.setValue1(new Long(hocVienId)); 
 		       con.setOperator("eq"); 
 		       cons.add(con); 
 		   }
 		   if (hocKyId != 0){
 			   ConditionReport con = new ConditionReport(); 
 		       con.setCol("hocKyId.id"); 
 		       con.setValue1(new Long(hocKyId)); 
 		       con.setOperator("eq"); 
 		       cons.add(con); 
 		   }
 		   List<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs = tblQLDTTXAHocVienPhieuDangKyMonHocService.findtblQLDTTXAHocVienPhieuDangKyMonHocByCondition(cons, 0, 0);
 		  if (tblQLDTTXAHocVienPhieuDangKyMonHocs.size() > 0){
 			 tblQLDTTXAHocVienPhieuDangKyMonHoc = tblQLDTTXAHocVienPhieuDangKyMonHocs.get(0);
 		  }
        }
        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 
        tblQLDTTXAMonHocBatBuocService = (TblQLDTTXAMonHocBatBuocService) context.getBean("tbl_QLDTTXA_MonHocBatBuocClient");
        tblQLDTTXAMonHocTienQuyetService = (TblQLDTTXAMonHocTienQuyetService) context.getBean("tbl_QLDTTXA_MonHocTienQuyetClient");
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");

        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
	   if (tblQLDTTXAHocVienPhieuDangKyMonHoc != null) 
	   { 
	       ConditionReport con = new ConditionReport(); 
	       con.setCol("phieuDangKyId.id"); 
	       con.setValue1(tblQLDTTXAHocVienPhieuDangKyMonHoc.getId()); 
	       con.setOperator("eq"); 
	       cons.add(con); 
	   } 
	   tblQLDTTXAHocVienDangKyMonHocs = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, pageSize, page); 
	   tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);

	   //get lop mon hoc du kien theo hocKyId
       cons.clear();
       if(tblQLDTTXAHocVienPhieuDangKyMonHoc != null && tblQLDTTXAHocVienPhieuDangKyMonHoc.getHocKyId() != null){ 
           ConditionReport con = new ConditionReport(); 
           con.setCol("hocKyId.id");
           con.setValue1(tblQLDTTXAHocVienPhieuDangKyMonHoc.getHocKyId().getId());
           con.setOperator("eq");
           cons.add(con);	
       } 
       List<TblQLDTTXALopMonHocDuKien> tblQLDTTXALopMonHocDuKiens = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
       
       tblQLDTTXAMonHocs = new ArrayList<TblQLDTTXAMonHoc>();
       tblQLDTTXAMonHocThieuMonTienQuyets = new ArrayList<TblQLDTTXAMonHoc>();
       tblQLDTTXAMonHocThieuMonHocTruocs = new ArrayList<TblQLDTTXAMonHoc>();
       List<Long> monHocIds = new ArrayList<Long>();
       for(TblQLDTTXALopMonHocDuKien tblLopDuKien : tblQLDTTXALopMonHocDuKiens){
    	   if(!monHocIds.contains(tblLopDuKien.getMonHocId().getId())){
   		   tblQLDTTXAMonHocs.add(tblLopDuKien.getMonHocId());
   		   monHocIds.add(tblLopDuKien.getMonHocId().getId());
   	   }    	   
       	
       }
       cons.clear();
       for (TblQLDTTXAMonHoc monHoc : tblQLDTTXAMonHocs){
   		ConditionReport con = new ConditionReport(); 
           con.setCol("monHocId.id");
           con.setValue1(monHoc.getId());
           con.setOperator("eq");
           cons.add(con);	
           
           // Kiem tra mon hoc truoc
           List<TblQLDTTXAMonHocBatBuoc> monHocBatBuocs = tblQLDTTXAMonHocBatBuocService.findtblQLDTTXAMonHocBatBuocByCondition(cons, 0, 0);
           for (TblQLDTTXAMonHocBatBuoc monHocBatBuoc : monHocBatBuocs){
           	List<ConditionReport> cons1 = new ArrayList<ConditionReport>(); 
           	
           	ConditionReport con0 = new ConditionReport(); 
               con0.setCol("hocVienId.id");
               con0.setValue1(tblQLDTTXAHocVienPhieuDangKyMonHoc.getHocVienId().getId());
               con0.setOperator("eq");
               cons1.add(con0);
               
           	ConditionReport con1 = new ConditionReport(); 
               con1.setCol("lopMonHocId.monHocId.id");
               con1.setValue1(monHocBatBuoc.getMonHocBatBuocId().getId());
               con1.setOperator("eq");
               cons1.add(con1);
               
               ConditionReport con2 = new ConditionReport(); 
               con2.setCol("trangThai");
               con2.setValue1(new Integer(1));
               con2.setOperator("eq");
               cons1.add(con2);
               
           	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons1);
           	if (num == 0){
           		//tblQLDTTXAMonHocs.remove(monHoc);
           		tblQLDTTXAMonHocThieuMonHocTruocs.add(monHoc);
           		break;
           	}
           }
           
           //Kiem tra mon hoc tien quyet
           List<TblQLDTTXAMonHocTienQuyet> monHocTienQuyets = tblQLDTTXAMonHocTienQuyetService.findtblQLDTTXAMonHocTienQuyetByCondition(cons, 0, 0);
           for (TblQLDTTXAMonHocTienQuyet monHocTienQuyet : monHocTienQuyets){
           	List<ConditionReport> cons1 = new ArrayList<ConditionReport>(); 
           	
           	ConditionReport con0 = new ConditionReport(); 
               con0.setCol("hocVienId.id");
               con0.setValue1(new Long(tblQLDTTXAHocVienPhieuDangKyMonHoc.getHocVienId().getId()));
               con0.setOperator("eq");
               cons1.add(con0);
               
           	ConditionReport con1 = new ConditionReport(); 
               con1.setCol("lopMonHocId.monHocId.id");
               con1.setValue1(monHocTienQuyet.getMonHocTienQuyetId().getId());
               con1.setOperator("eq");
               cons1.add(con1);
               
               ConditionReport con2 = new ConditionReport(); 
               con2.setCol("trangThai");
               con2.setValue1(new Integer(1));
               con2.setOperator("eq");
               cons1.add(con2);
               
               ConditionReport con3 = new ConditionReport(); 
               con3.setCol("tongDiem");
               con3.setValue1(new Double(5.0));
               con3.setOperator("geq");
               cons1.add(con3);
           	int num = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons1);
           	if (num == 0){
           		//tblQLDTTXAMonHocs.remove(monHoc);
           		tblQLDTTXAMonHocThieuMonTienQuyets.add(monHoc);
           		break;
           	}
           }
       }
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
